/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron.Actions;

import YottaTron.Runner;
import YottaTron.Sprites.Sprite;

/**
 *
 * @author Cenda
 */
public class BrakeAction extends Action {
float totalTime = 0;
    int direction;

    public BrakeAction(Sprite s, float startingTime, Runner r) {
        sprite = s;
        this.startingTime = startingTime;
        this.r = r;
        type = Action.BRAKE_ACTION;
        iterations = 0;
        if (sprite.getSpeed() > 0) {
            direction = 1;
        } else {
            direction = -1;
        }
        saveToHistory = true;
    }

    public boolean nextStep(float deltaTime) {
        //System.out.println(sprite.getSpeed());
        if (sprite.getSpeed() <= 0 && direction == 1) {
            sprite.setSpeed(0);
            sprite.variableChanged = true;
           
            finishTime = r.globalTimer.getTime();
            return false;

        }
        if (sprite.getSpeed() >= 0 && direction == -1) {
            sprite.setSpeed(0);
            sprite.variableChanged = true;
           
            finishTime = r.globalTimer.getTime();
            return false;
        }


         if (finished) {
            

            return false;
        }
         
        if (iterations == 0) {
       
        } else {
            totalTime += deltaTime;
            sprite.setSpeed(sprite.getSpeed() - sprite.getBrakePower() * deltaTime * direction);
            sprite.variableChanged = true;
        }

        
        iterations++;
        return true;

    }
    
}
